<script>
	import {
		HTTP_REQUEST_URL
	} from './config/app';
	// import {
	// 	getShopConfig,
	// 	silenceAuth
	// } from '@/api/public';
	import {
		mapGetters
	} from "vuex"
	// #ifdef MP
	// import Auth from '@/libs/wechat.js';
	// import Routine from './libs/routine.js';
	// let livePlayer = requirePlugin('live-player-plugin')
	// #endif

	export default {
		globalData: {
			spid: 0,
			code: 0,
			isLogin: false,
			userInfo: {},
			MyMenus: [],
			globalData: false,
			isIframe: false,
			tabbarShow: true,
			windowHeight: 0
		},
		computed: mapGetters(['isLogin', 'cartNum']),
		onLaunch: async function(option) {
			let that = this;
			if (option.query.spread) {
				that.$Cache.set('spread', option.query.spread);
				that.globalData.spid = option.query.spread;
				that.globalData.pid = option.query.spread;
			}
			// #ifdef APP-PLUS || H5
			uni.getSystemInfo({
				success: function(res) {
					// 首页没有title获取的整个页面的高度，里面的页面有原生标题要减掉就是视口的高度
					// 状态栏是动态的可以拿到 标题栏是固定写死的是44px
					let height = res.windowHeight - res.statusBarHeight - 44
					// #ifdef H5 || APP-PLUS
					that.globalData.windowHeight = res.windowHeight + 'px'
					// #endif
					// // #ifdef APP-PLUS
					// that.globalData.windowHeight = height + 'px'
					// // #endif

				}
			});
			// #endif	
			// #ifdef MP
			if (HTTP_REQUEST_URL == '') {
				console.error(
					"请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
				);
				return false;
			}
			if (option.query.hasOwnProperty('scene')) {
				switch (option.scene) {
					//扫描小程序码
					case 1047:
						let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
						that.globalData.code = val.pid === undefined ? val : val.pid;
						break;
						//长按图片识别小程序码
					case 1048:
						that.globalData.code = option.query.scene;
						break;
						//手机相册选取小程序码
					case 1049:
						that.globalData.code = option.query.scene;
						break;
						//直接进入小程序
					case 1001:
						that.globalData.spid = option.query.scene;
						break;
				}
			}
			const updateManager = wx.getUpdateManager();

			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
			});

			updateManager.onUpdateReady(function() {
				wx.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success: function(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					}
				});
			});

			updateManager.onUpdateFailed(function() {
				return that.Tips({
					title: '新版本下载失败'
				});
			});
			// #endif
			// getShopConfig().then(res => {
			// 	this.$store.commit('SETPHONESTATUS', res.data.status);
			// });
			// 获取导航高度；
			uni.getSystemInfo({
				success: function(res) {
					that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
				}
			});
			// #ifdef MP
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
			// #endif

			// #ifdef H5
			// 添加crmeb chat 统计
			var __s = document.createElement('script');
			__s.src = `${HTTP_REQUEST_URL}/api/get_script`;
			document.head.appendChild(__s);

			uni.getSystemInfo({
				success(e) {
					/* 窗口宽度大于420px且不在PC页面且不在移动设备时跳转至 PC.html 页面 */
					if (e.windowWidth > 420 && !window.top.isPC && !/iOS|Android/i.test(e.system)) {
						window.location.pathname = '/static/html/pc.html';
					}
				}
			});
			if (option.query.hasOwnProperty('type')) {
				this.globalData.isIframe = true;
			} else {
				this.globalData.isIframe = false;
			}

			if (window.location.pathname !== '/' && !this.isWork()) {
				let snsapiBase = 'snsapi_base';
				let urlData = location.pathname + location.search;
				if (!that.$store.getters.isLogin && uni.getStorageSync('authIng')) {
					uni.setStorageSync('authIng', false)
				}
				if (!that.$store.getters.isLogin) {
					let code,
						state,
						scope = ''

					if (option.query.code instanceof Array) {
						code = option.query.code[option.query.code.length - 1]
					} else {
						code = option.query.code
					}


					if (code && code != uni.getStorageSync('snsapiCode') && location.pathname.indexOf(
							'/pages/users/wechat_login/index') === -1) {
						// 存储静默授权code
						// uni.setStorageSync('snsapiCode', code);
						// let spread = that.globalData.spid ? that.globalData.spid : '';

						// try {
						// 	let res = await silenceAuth({
						// 		code: code,
						// 		snsapi: 'snsapi_base',
						// 		spread: that.$Cache.get('spread'),
						// 		spid: that.globalData.code
						// 	});
						// 	uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
						// 		.back_url)));
						// 	if (res.data.key !== undefined && res.data.key) {
						// 		this.$Cache.set('snsapiKey', res.data.key);
						// 	} else {
						// 		let time = res.data.expires_time - this.$Cache.time();
						// 		this.$store.commit('LOGIN', {
						// 			token: res.data.token,
						// 			time: time
						// 		});

						// 		this.$store.commit('SETUID', res.data.userInfo.uid);
						// 		this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
						// 		if (option.query.back_url) {
						// 			location.replace(decodeURIComponent(decodeURIComponent(option.query
						// 				.back_url)));
						// 		}
						// 	}
						// } catch (e) {
						// 	let url = ''
						// 	if (option.query.back_url instanceof Array) {
						// 		url = option.query.back_url[option.query.back_url.length - 1]
						// 	} else {
						// 		url = option.query.back_url
						// 	}
						// 	if (!that.$Cache.has('snsapiKey')) {
						// 		if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
						// 			Auth.oAuth('snsapi_userinfo', url);
						// 		}
						// 	}
						// }



						// silenceAuth({
						// 		code: code,
						// 		spread: that.$Cache.get('spread'),
						// 		spid: that.globalData.code
						// 	})
						// 	.then(res => {
						// 		uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query
						// 			.back_url)));
						// 		if (res.data.key !== undefined && res.data.key) {
						// 			this.$Cache.set('snsapiKey', res.data.key);
						// 		} else {
						// 			let time = res.data.expires_time - this.$Cache.time();
						// 			this.$store.commit('LOGIN', {
						// 				token: res.data.token,
						// 				time: time
						// 			});

						// 			this.$store.commit('SETUID', res.data.userInfo.uid);
						// 			this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
						// 			if (option.query.back_url) {
						// 				location.replace(decodeURIComponent(decodeURIComponent(option.query
						// 					.back_url)));
						// 			}
						// 		}
						// 	})
						// 	.catch(error => {
						// 		let url = ''
						// 		if (option.query.back_url instanceof Array) {
						// 			url = option.query.back_url[option.query.back_url.length - 1]
						// 		} else {
						// 			url = option.query.back_url
						// 		}
						// 		if (!that.$Cache.has('snsapiKey')) {
						// 			if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
						// 				Auth.oAuth(snsapiBase, url);
						// 			}
						// 		}
						// 	});
					} else {
						if (!this.$Cache.has('snsapiKey')) {
							if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
								Auth.oAuth(snsapiBase, urlData);
							}
						}
					}
				} else {
					if (option.query.back_url) {
						location.replace(uni.getStorageSync('snRouter'));
					}
				}
			}
			// #endif
			// #ifdef MP
			// 小程序静默授权
			// if (!this.$store.getters.isLogin) {
			// 	Routine.getCode()
			// 		.then(code => {
			// 			this.silenceAuth(code);
			// 		})
			// 		.catch(res => {
			// 			uni.hideLoading();
			// 		});
			// }
			// #endif
		},
		onShow(options) {
			let that = this;
			//直播间分享
			// #ifdef MP
			// const sceneList = [1007, 1008, 1014, 1044, 1045, 1046, 1047, 1048, 1049, 1073, 1154, 1155];
			//  if (sceneList.includes(options.scene)) {
			// 	livePlayer.getShareParams()
			// 		.then(res => {
			// 			//记录推广人uid
			// 			if(res.custom_params.pid){
			// 				 that.$Cache.set('spread', res.custom_params.pid);
			// 				 that.globalData.spid = res.custom_params.pid;
			// 				 that.globalData.pid = res.custom_params.pid;
			// 			}
			// 		}).catch(err => {
			// 		})
			// }
			// #endif
		},
		mounted() {
			
		},
		methods: {
			// 小程序静默授权
			isWork() {
				return navigator.userAgent.toLowerCase().indexOf('wxwork') !== -1 && navigator.userAgent.toLowerCase()
					.indexOf("micromessenger") !== -1
			}
		},
		onHide: function() {

		}
	};
</script>

<style lang="scss">
	@import 'uview-ui/theme.scss';
	@import url('@/plugin/emoji-awesome/css/google.min.css');
	@import url('@/plugin/animate/animate.min.css');
	@import 'static/css/base.css';
	@import 'static/iconfont/iconfont.css';
	@import 'static/css/guildford.css';
	@import 'static/css/style.scss';
	
	#app{
		font-family: "Microsoft Yahei";
	}
	
	
	@font-face {
		font-family: 'Alibaba-PuHuiTi-R';
		src: url('https://puhuiti.oss-cn-hangzhou.aliyuncs.com/AlibabaPuHuiTi/Alibaba-PuHuiTi-Regular/Alibaba-PuHuiTi-Regular.ttf') format('truetype');
		font-display: swap;
	}
	@font-face {
		font-family: 'Alibaba-PuHuiTi-B';
		src: url('https://puhuiti.oss-cn-hangzhou.aliyuncs.com/AlibabaPuHuiTi/Alibaba-PuHuiTi-Bold/Alibaba-PuHuiTi-Bold.ttf') format('truetype');
		font-display: swap;
	}
	@font-face {
		font-family: 'Alibaba-PuHuiTi-H';
		src: url('https://puhuiti.oss-cn-hangzhou.aliyuncs.com/AlibabaPuHuiTi/Alibaba-PuHuiTi-Heavy/Alibaba-PuHuiTi-Heavy.ttf') format('truetype');
		font-display: swap;
	}
	@font-face {
		font-family: 'Alibaba-PuHuiTi-L';
		src: url('https://puhuiti.oss-cn-hangzhou.aliyuncs.com/AlibabaPuHuiTi/Alibaba-PuHuiTi-Light/Alibaba-PuHuiTi-Light.ttf') format('truetype');
		font-display: swap;
	}
	@font-face {
		font-family: 'Alibaba-PuHuiTi-M';
		src: url('https://puhuiti.oss-cn-hangzhou.aliyuncs.com/AlibabaPuHuiTi/Alibaba-PuHuiTi-Medium/Alibaba-PuHuiTi-Medium.ttf') format('truetype');
		font-display: swap;
	}
	
	@font-face {
		font-family: 'Milky-Han-Mono-CN-Heavy';
		src: url('https://wugema.oss-cn-hangzhou.aliyuncs.com/font/XingHanDengKuan.ttf') format('truetype');
		font-display: swap;
	}
	

	view {
		box-sizing: border-box;
		line-height: 1;
	}

	page {
		font-family: PingFang SC;
	}

	.bg-color-red {
		background-color: var(--view-theme) !important;
	}

	.syspadding {
		padding-top: var(--status-bar-height);
	}
	
	.noData{
		width: 100%;
	}

	.flex {
		display: flex;
	}
	
	.flex-1{
		flex: 1;
	}
	
	.flex-align-center{
		display: flex;
		align-items: center;
	}
	
	.fixed-box {	
		.header-title {
			font-size: 32rpx !important;
			font-family: PingFangSC-Semibold !important;
			font-weight: 500 !important;
	
	
			.head-wrapper {
				.head-menu {
					.iconfont {
						&.icon-xiangzuo {
							font-size: 36rpx !important;
						}
					}
				}
			}
	
		}
	}
	
	.NewjoinCart,.NewSelectSku{
		width: 144rpx;
		height: 48rpx;
		background: linear-gradient(135deg, #F56845 0%, #EB4D42 100%);
		border-radius: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		image{
			width: 96rpx !important;
			height: 24rpx !important;
		}
		.num {
			// min-width: 14rpx;
			background-color: #fff;
			color: #EB4D42;
			border-radius: 15px;
			position: absolute;
			right: -5rpx;
			top: -7rpx;
			font-size: 16rpx !important;
			padding: 2rpx 10rpx 0;
			border: 1px solid #EB4D42;
			display: flex;
			align-items: center;
			padding-bottom: 0 !important;
		}
	}
	
	.newPrice{
		font-size: 36rpx;
		font-family: Alibaba-PuHuiTi-B;
		font-weight: normal;
		color: #F22718;
		text{
			font-size: 24rpx;
			font-family: Alibaba-PuHuiTi-R;
		}
	}
	
	.shareMakeMoney{
		min-width: 148rpx;
		height: 40rpx;
		background: rgba(0,0,0,0.56);
		border-radius: 8rpx 8rpx 8rpx 0rpx;
		font-size: 24rpx;
		font-family: PingFangSC-Regular;
		font-weight: 400;
		color: #FFFFFF;
		display: flex;
		padding: 8rpx;
		align-items: center;
		justify-content: center;
		position: absolute;
		left: 8rpx;
		bottom: 8rpx;
	}
	
	// .shareMakeMoney{
	// 	width: fit-content;
	// 	height: 40rpx;
	// 	background: rgba(235,77,66,0.08);
	// 	border-radius: 4rpx;
	// 	display: flex;
	// 	align-items: center;
	// 	justify-content: center;
	// 	font-size: 24rpx;
	// 	font-family: PingFangSC-Regular;
	// 	font-weight: 400;
	// 	color: #EB4D42;
	// 	padding: 8rpx;
	// 	margin-top: 16rpx;
	// }

	.uni-scroll-view::-webkit-scrollbar {
		/* 隐藏滚动条，但依旧具备可以滚动的功能 */
		display: none;
	}

	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}

	.uni-system-open-location .map-content.fix-position {
		height: 100vh;
		top: 0;
		bottom: 0;
	}
	
	.flex-between-center{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.no-goods{
		text-align: center;
		padding-bottom: 60rpx!important;
		color: #999;
		image{
			width: 414rpx;
			height: 304rpx;
			margin: 40rpx auto 0 auto;
			display: block;
		}
	}
	
	.text-info{
		.title{
			max-height: 80rpx;
			line-height: 40rpx;
		}
	}
	
	.store_subtitle{
		font-size: 24rpx;
		font-family: PingFangSC-Regular;
		font-weight: 400;
		color: #999999;
		margin-top: 8rpx;
		margin-bottom: 16rpx;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden; 
	}
	
</style>
